import {cbs} from '../../../../config/settings.js';
import Upload from '../../../Common/Upload/';

module.exports = {
  name: 'add',
  data(){
    let checkNum = (rule, value, cb) => {
        if(!/^\d+$/.test(value)) {
            cb(new Error('请输入正整数'));
        }
        setTimeout(() => {
            if(!Number.isInteger(value*1) || value < 0) {
                cb(new Error('请输入正整数值'))
            }else {
                cb();
            }
        }, 0);
    };
  	return{
       brands:[],
       names:[],
       standards:[],
       // dialog
       dialog: {
          title: '自定义上传商品',
          show: false,
          modal: false
       },
       // 上传已有商品
       form: {
          brand:'',
          name:'',
          water:'',
          quantity:''
       },
       // 自定义商品
       newProForm: {
          brand: '',
          name: '',
          standard: '',
          quantity: '',
          picUrl: '',
          standard: '',
          disposable: '',
          prForWaSt: '',
          waterFactoryName: ''
       },
       // 校验规则
       rules: {
          brand: [{
              required: true, message: '品牌不能为空'
          }],
          name: [{
              required: true, message: '名称不能为空'
          }],
          standard: [{
              required: true, message: '规格不能为空'
          }],
          water: [{
              required: true, message: '规格不能为空'
          }],
          quantity: [{
              required: true, message: '库存不能为空'
          }, {
              validator: checkNum, trigger: 'blur'
          }]
       },
       // 自定义商品校验规则
       cusRules: {
          brand: [{
              required: true, message: '品牌不能为空', trigger: 'blur'
          }],
          name: [{
              required: true, message: '名称不能为空', trigger: 'blur'
          }],
          quantity: [{
              required: true, message: '库存不能为空'
          }, {
              validator: checkNum, trigger: 'blur'
          }],
          standard: [{
              required: true, message: '规格不能为空', trigger: 'blur'
          }],
          picUrl: [{
              required: true, message: '请上传商品图片'
          }],
          disposable: '',
          prForWaSt: [{
              validator: (rule, value, cb) => {
                  if(!value) {
                      cb(new Error('请输入商品价格'))
                  }else if(!/^\d+(\.\d+)?$/.test(value)) {
                      cb(new Error('只能输入数字'))
                  }else {
                      cb()
                  }
              }, trigger: 'blur'
          }],
          waterFactoryName: [{
              required: true, message: '水厂名不能为空', trigger: 'blur'
          }]
       },
       // 规格列表
       standards: [],
       // 水厂列表
       waterWorks: [],
       showWorkList: false,
       showStanList: false,
       hasSubmit: false,
       prefix: `${this.$store.state.user.userinfo.name}_商品_`
  	}
  },
  methods: {
      getBrands() {
        let data = this.$$formData({
              pageNum: 1,
              pageSize: 1000
          })
          this.$$api_product_brand(data, res => {
              if(res.event === 0) {
                 this.brands = res.obj;
              }else {
                  cbs.submitError.call(this, `获取品牌信息失败，${res.msg}`);
              }
          })
      },
      getAllNames() {
          let data = this.$$formData({
              brandKeyword: this.form.brand,
              pageNum: 1,
              pageSize: 1000
          })

          this.names = [];
          this.standards = [];
          this.form.name = '';
          this.form.water = '';

          this.$$api_product_name(data, res => {
              if(res.event === 0) {
                  this.names = res.obj;
              }else {
                  cbs.submitError.call(this, `获取该品牌名称信息失败`);
              }
          })

      },
      getAllStandards() {
          let data = this.$$formData({
              brand: this.form.brand,
              name: this.form.name,
              pageNum: 1,
              pageSize: 1000
          })

          this.standards = [];
          this.form.water = '';

          this.$$api_product_standard(data, res => {
              if(res.event === 0) {
                  this.standards = res.obj;
              }else {
                  cbs.submitError.call(this, `获取规格信息失败`);
              }
          })
      },
      addNew() {
          this.dialog.show = true;
      },
      submitPro(formName) {
          this.$refs[formName].validate(valid => {
              if(valid) {

                  let data = this.$$formData({
                      waterBucketId: this.form.water.bucketTypeId,
                      quantity: this.form.quantity
                  })

                  this.$$api_product_uploadExit(data, res => {
                      if(res.event === 0) {
                          this.$router.push('/product/list');
                      }else if(res.event === 4) {
                          cbs.submitError.call(this, `提交失败，您已上传过该商品`);
                      }else {
                          cbs.submitError.call(this, `提交失败，${res.msg}`);
                      }
                  })
              }else {
                  return false;
              }
          })
      },
      submitCusPro(formName) {
          this.$refs[formName].validate(valid => {
              if(valid) {
                  this.hasSubmit = true;

                  let data = {}
                  Object.assign(data,this.newProForm);
                  data = this.$$formData(data);

                  this.$$api_product_uploadNew(data, res => {
                      if(res.event === 0) {
                          this.$router.push('/product/list');
                          cbs.submitSuccess.call(this, `上传成功`);
                      }else if(res.event === 1) {
                          cbs.submitError.call(this, `添加商品失败`);
                      }else {
                           cbs.submitError.call(this, `提交失败，${res.msg}`);
                      }
                      this.hasSubmit = false;
                  }, {
                      errFn: err => {
                          this.hasSubmit = false;
                          cbs.submitError.call(this, `提交失败，${res.msg}`);
                      }
                  })
              }else {
                  console.log('error');
                  this.hasSubmit = false;
                  return false;
              }
          })
      },
      getWorksList() {
          let data = this.$$formData( {
              keyword: this.newProForm.waterFactoryName,
              pageSize: 10
          })

          this.$$api_product_getWorkList(data, res => {
              if(res.event === 0) {
                  this.waterWorks = res.obj;
              }else {
                  cbs.submitError.call(this, `获取水厂信息失败，${res.msg}`);
              }
          })
      },
      getStanList() {
          let data = this.$$formData( {
          })

          this.$$api_product_getStanList(data, res => {
              if(res.event === 0) {
                  this.standards = res.obj;
                  this.onShowList('showStanList');
              }else {
                  cbs.submitError.call(this, `获取规格信息失败，${res.msg}`);
              }
          })
      },
      choseWork(item) {
          this.newProForm.waterFactoryId = item.factoryId;
          this.newProForm.waterFactoryName = item.name;
      },
      onchangeWork() {
        this.newProForm.waterFactoryId = '';
      },
      choseStandard(stan) {
          this.newProForm.standard = stan;
      },
      reset(name) {
          this.$refs[name].resetFields();
      },
      onShowList(name) {
          this[name] = true;
      },
      onCloseList(name) {
          this[name] = false;
      },
      onInputBlur(name) {
          setTimeout(() => {
              this.onCloseList(name);
          }, 300)
      },
      onGetProductUrl(val) {
          this.newProForm.picUrl = val
      },
      setSize() {
          $('.dialog-form .el-dialog').css('width', '480px');
      }
  },
  created() {
      this.getBrands();
  },
  components: {
      Upload
  },
  mounted() {
    this.setSize()
  },
  computed: {
      proPrice() {
          return this.form.water.prForWaSt || 0;
      },
  },
}